package counsel;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.SerialDAO;
import common.SerialDTO;
import contract.ContractDAO;
import contract.ContractDTO;

public class CounselInsertOK extends HttpServlet{

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		req.setCharacterEncoding("utf-8");
		
		//상담번호 채번
		SerialDTO sDto = new SerialDTO("COUNSEL","CS_no","C");
		SerialDAO sDao = new SerialDAO();
		sDao.setDto(sDto);
		String cs_no = sDao.serialReturn();						//상담 번호
		String sa_no = req.getParameter("sa_no");				//사원 번호
		String pd_no = req.getParameter("pd_no");				//상품 번호
		String cs_content = req.getParameter("cs_content");		//상담 내용
		String cs_loan = req.getParameter("cs_loan");			//대출 신청액
		String cs_max = req.getParameter("cs_max");				//대출 한도
		String sa_dpm = req.getParameter("sa_dpm");				//부서
		String sa_gd = req.getParameter("sa_gd");				//작책
		String pd_pback = req.getParameter("pd_pback");			//납입 구분코드
		String pd_rate = req.getParameter("pd_rate");			//금리
		String pd_prd = req.getParameter("pd_prd");				//상환 기간
		String rg_pp = (String)req.getSession().getAttribute("mid");
		cs_max = cs_max.replace("만원", "");
		
		CounselDTO coun = new CounselDTO(cs_no, sa_no, pd_no, cs_content, cs_loan, cs_max, 
						sa_dpm, sa_gd, pd_pback, pd_rate, pd_prd, rg_pp, rg_pp);
		CounselDAO counDAO = new CounselDAO();
		counDAO.setCoun(coun);
		int flag = counDAO.insert();
		sDto = new SerialDTO("contract", "ct_no", "L");
		sDao = new SerialDAO();
		sDao.setDto(sDto);
		String ct_no = sDao.serialReturn();
		ContractDTO cont = new ContractDTO(ct_no, cs_no, "01", pd_pback, pd_rate, cs_loan, pd_prd, rg_pp, rg_pp);
		ContractDAO contDAO = new ContractDAO();
		contDAO.setCont(cont);
		flag = contDAO.insert();
		req.setAttribute("Page", "IN");
		req.setAttribute("Flag", flag);
		
		//req.getRequestDispatcher("/Coun/CounList.clamp").forward(req, resp);
		//resp.sendRedirect(req.getContextPath()+"/intranet/main/invalidate.jsp?url="+req.getContextPath()+"/Coun/CounList.clamp");
		req.getRequestDispatcher("/intranet/counsel/message.jsp").forward(req, resp);
	}

}
